//
//  YZMCityListViewController.m
//  YZMicroStock
//
//  Created by wangminhong on 16/4/19.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMCityListViewController.h"
#import "YZMProvince.h"
#import "YZMSeparatorCell.h"
#import "YZMCity.h"

@interface YZMCityListViewController()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) YZMProvince *province;

@end

@implementation YZMCityListViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title = _province.name;
    self.view.backgroundColor = [YZMAppStyle appBackgroundColor];
    
    [self createObjects];
}


#pragma mark -- UITableViewDataSource

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *identifier = @"BankCardCell";
    YZMSeparatorCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[YZMSeparatorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        cell.accessoryType = UITableViewCellAccessoryNone;
        
        cell.textLabel.textColor = [YZMAppStyle colorForMainTextColor];
        cell.textLabel.font = [YZMAppStyle mediumSizeFont];
    }
    
    YZMCity *instance = self.cityList[indexPath.row];
    cell.textLabel.text = instance.name;
    
    YZMIndexCount indexCount;
    
    [cell.contentView insertSubview:cell.textLabel belowSubview:cell.bottomLineView];
    indexCount.index = indexPath.row;
    indexCount.count = self.cityList.count;
    cell.indexCount = indexCount;
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.cityList.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (self.delegate) {
        
        [self.delegate provinceDidChoose:_province andCity:self.cityList[indexPath.row]];
    }
    
    [YZMHCMInstance popViewController:self.delegate animated:YES];
}

- (void)createObjects{
    
    _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.edges.equalTo(self.view);
    }];
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
    
    UILabel *headerLB = [UILabel new];
    headerLB.text = @"选择城市";
    headerLB.textColor = [YZMAppStyle colorForWhite];
    headerLB.font = [YZMAppStyle smallSizeFont];
    headerLB.userInteractionEnabled = YES;
    [view addSubview:headerLB];
    [headerLB mas_makeConstraints:^(MASConstraintMaker *maker){
        
        maker.left.equalTo(view).offset(15);
        maker.bottom.equalTo(view).offset(-10);
    }];
    
    _tableView.tableHeaderView = view;
}

#pragma mark -- Params Manager

+ (instancetype)createWithParams: (NSDictionary *)params
{
    YZMCityListViewController *ctrl = [[self alloc] init];
    [ctrl updateWithParams: params];
    return ctrl;
}

- (void)updateWithParams:(NSDictionary *)params{
    
    _delegate = params[@"delegate"];
    _cityList = params[@"cityList"];
    _province = params[@"province"];
}

@end
